package com.bhs.sansonglogistics.ui.enterprise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.CompanyPhotoBean;
import com.bhs.sansonglogistics.bean.PictureBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.PictureSelectAdapter;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.luck.picture.lib.entity.LocalMedia;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 公司照片
 */
public class CompanyPhotoUploadingActivity extends BaseActivity implements NetCallBack {


    private RecyclerView rvCompanyPicture;
    private RecyclerView rvStaffPresence;
    private RecyclerView rvTransportCapacity;
    private RecyclerView rvQualificationCertificate;
    private Button btnSubmit;
    private PictureSelectAdapter companyPictureAdapter;
    private PictureSelectAdapter staffPresenceAdapter;
    private PictureSelectAdapter transportCapacityAdapter;
    private PictureSelectAdapter qualificationCertificateAdapter;
    private CompanyPhotoBean photoBean;
    //图片选择的最大数量
    private int maxSelectNum = 9;

    @Override
    public int initLayout() {
        return R.layout.activity_company_photo_uploading;
    }

    @Override
    public void initView() {

        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("公司照片");
        rvCompanyPicture = findViewById(R.id.rv_company_picture);
        rvStaffPresence = findViewById(R.id.rv_staff_presence);
        rvTransportCapacity = findViewById(R.id.rv_transport_capacity);
        rvQualificationCertificate = findViewById(R.id.rv_qualification_certificate);
        btnSubmit = findViewById(R.id.btn_submit);
    }

    @Override
    public void initData() {
        companyPictureAdapter = new PictureSelectAdapter(maxSelectNum, this);
        rvCompanyPicture.setAdapter(companyPictureAdapter);
        rvCompanyPicture.setLayoutManager(new GridLayoutManager(this, 3));
        staffPresenceAdapter = new PictureSelectAdapter(maxSelectNum, this);
        rvStaffPresence.setAdapter(staffPresenceAdapter);
        rvStaffPresence.setLayoutManager(new GridLayoutManager(this, 3));
        transportCapacityAdapter = new PictureSelectAdapter(maxSelectNum, this);
        rvTransportCapacity.setAdapter(transportCapacityAdapter);
        rvTransportCapacity.setLayoutManager(new GridLayoutManager(this, 3));
        qualificationCertificateAdapter = new PictureSelectAdapter(maxSelectNum, this);
        rvQualificationCertificate.setAdapter(qualificationCertificateAdapter);
        rvQualificationCertificate.setLayoutManager(new GridLayoutManager(this, 3));

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
        networkRequest(netApi.getCompanyImg(), this, 9527);


    }

    private void submit() {
        Map<String, String> map = new ArrayMap<>();
        map.put("company_pics", companyPictureAdapter.getImages());
        map.put("staff_pics", staffPresenceAdapter.getImages());
        map.put("capacity_pics", transportCapacityAdapter.getImages());
        map.put("credentials_pics", qualificationCertificateAdapter.getImages());
        networkRequest(netApi.uploadCompanyImg(map), this);
    }

    private List<PictureBean> companyList = new ArrayList<>();
    private List<PictureBean> staffList = new ArrayList<>();
    private List<PictureBean> capacityList = new ArrayList<>();
    private List<PictureBean> credentialsList = new ArrayList<>();

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 9527) {
            photoBean = new Gson().fromJson(json, CompanyPhotoBean.class);
            if (photoBean.isStatus()) {

                CompanyPhotoBean.DataDTO.CompanyImgsDTO data = photoBean.getData().getCompany_imgs();
                for (String company_pic : data.getCompany_pics()) {
                    companyList.add(new PictureBean(company_pic, company_pic));
                    companyPictureAdapter.localMedia.add(LocalMedia.generateHttpAsLocalMedia(MyUtils.watermark(company_pic)));
                }

                if (companyPictureAdapter.getMaxSelectNum() > companyList.size()) {
                    companyList.add(new PictureBean(2));
                }
                companyPictureAdapter.setNewData(companyList);


                for (String company_pic : data.getStaff_pics()) {
                    staffList.add(new PictureBean(company_pic, company_pic));
                    staffPresenceAdapter.localMedia.add(LocalMedia.generateHttpAsLocalMedia(MyUtils.watermark(company_pic)));
                }

                if (staffPresenceAdapter.getMaxSelectNum() > staffList.size()) {
                    staffList.add(new PictureBean(2));
                }
                staffPresenceAdapter.setNewData(staffList);

                for (String company_pic : data.getCapacity_pics()) {
                    capacityList.add(new PictureBean(company_pic, company_pic));
                    transportCapacityAdapter.localMedia.add(LocalMedia.generateHttpAsLocalMedia(MyUtils.watermark(company_pic)));
                }

                if (transportCapacityAdapter.getMaxSelectNum() > capacityList.size()) {
                    capacityList.add(new PictureBean(2));
                }
                transportCapacityAdapter.setNewData(capacityList);


                for (String company_pic : data.getCredentials_pics()) {
                    credentialsList.add(new PictureBean(company_pic, company_pic));
                    qualificationCertificateAdapter.localMedia.add(LocalMedia.generateHttpAsLocalMedia(MyUtils.watermark(company_pic)));
                }

                if (qualificationCertificateAdapter.getMaxSelectNum() > credentialsList.size()) {
                    credentialsList.add(new PictureBean(2));
                }
                qualificationCertificateAdapter.setNewData(credentialsList);

            }
        } else {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                finish();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}